﻿// CStateManager.cs
// Author: Gant Briseno
#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Casters.source.States;
#endregion

namespace Casters.source.Managers
{
    static public class CStateManager
    {

        public static List<CBaseState> m_ListOfStates = new List<CBaseState>();

        static public int ListSize { get { return m_ListOfStates.Count; } }

        /// <summary>
        /// Loads all the resources for all states in the list
        /// </summary>
        static public void Initialize()
        {
            CEventSystem.RegisterEventHandler(EventID.ChangeState, OnChangeState);
        }

        /// <summary>
        /// Unloads all of the resources for all states in the list
        /// </summary>
        static public void Shutdown()
        {
            for (int i = m_ListOfStates.Count() - 1; i >= 0; --i)
            {
                m_ListOfStates[i].UnloadContent();
                m_ListOfStates.RemoveAt(i);
            }
        }

        /// <summary>
        /// Add a state to the list and initialize it
        /// </summary>
        /// <param name="_state">State to be added</param>
        static public void AddState(CBaseState _state)
        {
            _state.Initialize();
            _state.LoadContent();

            m_ListOfStates.Add(_state);
        }

        /// <summary>
        /// Clears out the list and pushes on the new state
        /// </summary>
        /// <param name="eventArgs"></param>
        static public void OnChangeState(params object[] eventArgs)
        {
            CBaseState state = (CBaseState)eventArgs[0];
            Shutdown();
            AddState(state);
        }

        /// <summary>
        /// Unloads and removes a state
        /// </summary>
        /// <param name="_state">State to be removed</param>
        static public void RemoveState(CBaseState _state)
        {
            _state.UnloadContent();
            m_ListOfStates.Remove(_state);
        }

        /// <summary>
        /// Updates the logic for the top most state only
        /// </summary>
        /// <param name="dt">Delta Time</param>
        static public void Update(float dt)
        {
            if (m_ListOfStates.Count > 0)
                m_ListOfStates[m_ListOfStates.Count - 1].Update(dt);
        }

        static public void Draw()
        {
            foreach (CBaseState state in m_ListOfStates)
                state.Draw();
        }
    }
}
